#!/bin/sh

SYSCONF_FILE='/mnt/rootfs/ket_boardcfg/system.json'
IP_CONF='/etc/network/interfaces'
address_token="address "
gateway_token="gateway "
netmask_token="netmask "

jq_ip="ip"
jq_gateway="gateway"
jq_netmask="netmask"

compare(){
	which_net=""
	which_item=""
	jq_key=""
	which_token=""
	line_start=0
	base=1
	for i in  $(seq 1 2)  
	
	do
		if [ $i -eq 1 ]; then
			which_net="net0."
			line_start=8
		else
			which_net="net1."
			line_start=15
		fi

		echo ${which_net}

		for j in $(seq 1 3)
		do
			if [ $j -eq 1 ]; then
				which_item=${address_token}
				jq_key=${jq_ip}
				which_token=${address_token}
			elif [ $j -eq 2 ]; then
				which_item=${netmask_token}
				jq_key=${jq_netmask}
				which_token=${netmask_token}
			else
				which_item=${gateway_token}
				jq_key=${jq_gateway} 
				which_token=${gateway_token}
			fi
			jqkey=${which_net}${jq_key}
			temp=`cat ${SYSCONF_FILE}|jq .${jqkey}`
			temp2=${temp//\"/}

			#json
			linecnt=${which_token}${temp2}
		
			
			linenum=$((line_start+j-1))
			lineinterface=`sed -n ${linenum}p ${IP_CONF}`
		
			if [ "$linecnt" = "$lineinterface" ];then
				echo "this line same"
			else 
				echo "~~~ not same"
			    sed "${linenum}c ${linecnt}" ${IP_CONF} > /etc/network/interface.tmp
			fi
			
		done
	done

	
}

	echo "finish done"
	echo $1
	
	if [ $1 == web ];then
		echo "handle upload web"
		unzip -o /web/dist.zip -d /web
		cp -rf /web/dist/* /web/
		rm -rf /web/dist.zip /web/dist/
		#rm -rf /tmp/0*
		chmod -R 777 /web
	elif [ $1 == logo.png ];then
		chmod a+r /web/img/logo.png
	elif [ $1 == designroom.png ]; then
		chmod a+r /web/img/designroom.png
	elif [ $1 == net ];then
		compare
	fi
	rm -rf /tmp/0*
